package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.other.config.exception.BusinessException;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.PetHomeConstant;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.SendEmailService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //交给spring管理
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {

    @Autowired
    SendEmailService sendEmailService;


    @Autowired
    RedisService redisService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //主体
        //1.新增员工，自动拿到员工的id
        Employee admin = shop.getAdmin();

        //细节
        //1.参数校验，两次密码校验
        if (!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new RuntimeException("两次密码不一致");
        }
        //2.账号验证
        List<Employee> employees = employeeMapper.selectByUsernameByPhoneByEmail(admin);

        //和其他账号冲突了
        if (employees.size() > 0){
            throw new RuntimeException("账号重复，有可能是账户名，邮箱，手机号其中一个或多个重复了");
        }


        //1.1加密加盐
        admin.init();

        employeeMapper.insertSelective(admin);

        //2.新增店铺信息
        //2.1把员工的id设置的店铺的admin_id
        shop.setAdminId(admin.getId());
        //2.2店铺信息入库，自动获取店铺主键
        shopMapper.insertSelective(shop);

        //3.把店铺的主键设置给员工对象，
        admin.setShopId(shop.getId());

        //3.1更新员工对象
        employeeMapper.updateByPrimaryKeySelective(admin);
        //redis保存数据 key就是uuid   value：店铺id
        String key = IdUtil.fastSimpleUUID();
        redisService.setStringKeyAndValue(60,key,shop.getId()+"");
        //发送邮件
        sendEmailService.send("宠物雷院激活","http://localhost/shop/jihuo/"+key,admin.getEmail());


    }

    @Override
    public void jihuo(String uuid) {

        //1.通过uuid作为key，从redis中获取数据
        String shopId = redisService.getKeyStringValue(uuid);
        //1.1如果获取不到，请联系管理员
        if (StringUtils.isBlank(shopId)){
            throw new BusinessException("激活链接无效了，请联系管理员");
        }

        //2.激活店铺（修改state = 2）
        Shop shop = new Shop();
        shop.setId(Long.valueOf(shopId));
        shop.setState(PetHomeConstant.Shop.JIHUO_CHENGGONG);

        shopMapper.updateByPrimaryKeySelective(shop);


    }

    public static void main(String[] args) {
/*        String s = MD5.create().digestHex("123456");
        System.out.println(s);*/
        //main(args);
        /*String salt = IdUtil.fastSimpleUUID();
        System.out.println(salt); //3bc22f23f2cd422aa9a5b1530d8c9eb0   //8c365f7a0cb9eaf378f8dcf2977cceef
                                  //f684956e82a942da82abd3a4fcccf5a9    //2e6afba4818e93d33579b441da3d169d
        String pwd = MD5.create().setSalt(salt.getBytes()).digestHex("123456");
        System.out.println(pwd);

        String pwd2 = MD5.create().setSalt("275e45021f9a4655aba09185fcab148c".getBytes()).digestHex("123");
        System.out.println(pwd2);*/

    }
}
